package com.dadaguai.common;

import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@Data
public class R<T> implements Serializable {
    private String state; //执行状态

    private Integer code; //编码：1成功，0和其它数字为失败

    private String message; //错误信息

    private T data; //数据

    private Map dataMap = new HashMap(); //动态数据

    public static <T> R<T> success(T object) {
        R<T> r = new R<T>();
        r.state = "success";
        r.data = object;
        r.code = 200;
        return r;
    }
    public static <T> R<T> success(String msg) {
        R<T> r = new R<T>();
        r.state = "success";
        r.message = msg;
        r.code = 200;
        return r;
    }
    public static <T> R<T> success() {
        R<T> r = new R<T>();
        r.state = "success";
        r.message = "ok";
        r.code = 200;
        return r;
    }

    public static <T> R<T> error(String msg,Integer code) {
        R r = new R();
        r.state = "error";
        r.message = msg;
        r.code = code;
        return r;
    }
    public static <T> R<T> error(String msg) {
        R r = new R();
        r.state = "error";
        r.message = msg;
        r.code = -1;
        return r;
    }

    public R<T> add(String key, Object value) {
        this.dataMap.put(key, value);
        return this;
    }

}
